package com.cb.controller.admin.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.command.QueryFilter;
import com.cb.core.command.QueryResult;
import com.cb.core.controller.BaseListController;
import com.cb.domain.model.system.AppResource;
import com.cb.domain.service.system.AppResourceManager;

/**
 * 
 * @Author author
 * @Vesion 1.0
 * Create Time:
 * <p><b>Class Description:</b>[TODO]<p>
 */
public class AppResourceController extends BaseListController {
	
	@Autowired
	private AppResourceManager appResourceManager;
	
	/**对象列表页，提供最基本的分页显示
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		QueryResult appResources=appResourceManager.getAll(new QueryFilter(request));
		
		return new ModelAndView("admin/system/appResourceList","appResources",appResources);
	}
	
	/**
	 * 批量删除，通过页面传回对象的主键数组进行逐条删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		String[] resourceIds=request.getParameterValues("resourceId");
		if(resourceIds!=null){
			for(String resourceId:resourceIds){
				appResourceManager.remove(new Long(resourceId));
			}
			saveMessage(request, getText("appResource.mutiDeleted"));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 删除单个实体对象
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView delete(HttpServletRequest request,	HttpServletResponse response){
		String resourceId=request.getParameter("resourceId");
		if(StringUtils.isNotEmpty(resourceId)){
			AppResource appResource=appResourceManager.get(new Long(resourceId));
			appResourceManager.remove(appResource);
			saveMessage(request,getText("appResource.deleted",new Object[]{appResource.getResourceId()}));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 查看实体对象详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView viewDetail(HttpServletRequest request,	HttpServletResponse response){
		String resourceId=request.getParameter("resourceId");
		AppResource appResource=null;
		if(StringUtils.isNotEmpty(resourceId)){
			appResource=appResourceManager.get(new Long(resourceId));
		}
		return new ModelAndView("admin/system/appResourceDetail","appResource",appResource);
	}
}
